<template>
	<view>
		<!-- 使用自定义组件 -->
		<my-search @handlesearch = "handlesearch"></my-search>
		<view class="scroll-view-contain">
			<!-- 左侧滑动区域 -->
			<scroll-view scroll-y="true" :style="{height: wh+'px'}" class="scroll-left">
				<view class="left-item " v-for="(item,index) in cateList" 
				:key = "item.cat_id" @click="changecruentindex(index)" :class="index===curentindex?'activeclass':''">{{item.cat_name}}
				</view>
			</scroll-view>
			<!-- 右侧滑动区域 -->
			<scroll-view scroll-y="true" :style="{height: wh+'px'}" :scroll-top="scrollTop">
				<view class="scroll-right" v-for="(item,index) in cateList2" :key="item.cat_id">
					<view class="scroll-right-title">
						/{{item.cat_name}}/
					</view>
					<view class="scroll-right-img">
						<view class="scroll-right-item"  v-for="(item1,index1) in item.children" :key="item1.cat_id" @click="gotogoodlist(item1)">
							<image :src="item1.cat_icon" mode="" :key="item1.cat_id"></image>
							<view class="scroll-right-desc">
								{{item1.cat_name}}
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		
	</view>
</template>

<script>
	import badge from '@/mixins/tabbar-badge.js'
	export default {
		mixins:[badge],
		data() {
			return {
				wh:0,//当前设备可用的高度
				cateList:[],//分类列表数据
				cateList2:[],//2级分类列表数据
				curentindex:0, //动态类的下标
				scrollTop:0//解决切换到不同列表位置问题
			}
		},
		methods: {
			async getcatelist(){
			  const {data:res} = await uni.$http.get('/api/public/v1/categories')
			  if(res.meta.status!=200){
				  uni.$showMsg('数据请求失败')
			  }else{
				  this.cateList = res.message
				  this.cateList2 = res.message[this.curentindex].children	  
			  }
			},
			//修改动态类的下标
			changecruentindex(index){
				this.curentindex = index
				this.getcatelist()
				this.scrollTop = this.scrollTop==0?1:0
			},
			gotogoodlist(item1){
				uni.navigateTo({
					url:'/subpkg/goods_liat/goods_liat?cid='+item1.cat_id
				})
			},
			// 跳转到搜索页面
			handlesearch(){
				uni.navigateTo({
					url:'../../subpkg/search/search'
				})
			}
		},
		onLoad() {
			//调用Api获取当前设备信息
			const sysinfo =  uni.getSystemInfoSync()
			this.wh = sysinfo.windowHeight-50
			// 请求分类列表数据
			this.getcatelist()
		}
	}
</script>

<style scoped>
.scroll-view-contain{
	display: flex;
}
.scroll-left{
	width: 120px;
}
.left-item{
	background-color: #F7F7F7;
	line-height: 60px;
	text-align: center;
	font-size: 12px;
}
.activeclass{
	background-color: #ffffff;	
	position: relative;
}
.activeclass::before{
	content: "";
	display: block;
	width: 3px;
	height: 30px;
	background-color: red;
	position: absolute;
	top: 50%;
	left: 0;
	transform: translateY(-50%);
}
.scroll-right-title{
	text-align: center;
	font-weight: 600;
	font-size: 14px;
	height: 60px;
	line-height: 60px;
}
.scroll-right-img{
	display: flex;
	flex-wrap: wrap;
	justify-content: flex-start;
}
.scroll-right-item{
	width: 33.3%;
	text-align: center;
	margin-bottom: 40rpx;
	font-size: 14px;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}
.scroll-right-img image{
	margin: 0 20rpx;
	width: 120rpx;
	height: 120rpx;
}
</style>
